import java.util.*;


public class Solution16 {
    public static void main(String[] args) {
        char[][] chars = {
                {'1','1','1','1','0'},
                {'1','1','0','1','0'},
                {'1','1','0','0','0'},
                {'0','0','0','0','0'}
        };
        Solution16 solution =new Solution16();
        int i = solution.numIslands(chars);
        System.out.println(i);
    }

    public int numIslands(char[][] grid) {
        //采用dfs
        int nums= 0;
        if(null == grid)return 0;
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[i].length;j++){
                if(grid[i][j] == '1'){
                    nums++;
                    _numIsLands(i,j,grid);
                }
            }
        }
        return nums;
    }
    private void _numIsLands(int i, int j, char[][] grid) {
        if(grid[i][j] == '1') {
            grid[i][j] = '0';
            int up = i - 1;
            int down = i + 1;
            int next = j + 1;
            int before = j - 1;
            if (up >= 0) {
                _numIsLands(up, j, grid);
            }
            if (down < grid.length) {
                _numIsLands(down, j, grid);
            }
            if (next < grid[i].length) {
                _numIsLands(i, next, grid);
            }
            if (before >= 0) {
                _numIsLands(i, before, grid);
            }
        }
    }


}
